namespace Vanilla.Tools.Calc

import Vanilla.UI
import Vanilla

class CalcWindow (MainWindow):
	// Our equivalent of a real calculator's screen
	public Screen as Vanilla.UI.Label
	
	// The last number
	public LastNumber as double = 0
	
	// Current operation
	public Operation as string = ""

	def constructor ():
		// Creates the window on the desktop and sets it's title
		super("Calculator")
		
		// Creates a column in which to place the contents
		c = Column(self)
		
		// Adds the screen, with a suitable font
		Screen = Vanilla.UI.Label(c, "0", Font("Arial", 30, FontSlant.Normal, FontWeight.Bold))
		
		// if the default font colour is light, make it bright green, otherwise make it dark green
		if Screen.Colour.R + Screen.Colour.G + Screen.Colour.B > 1.5:
			Screen.Colour = Colour(0,0.8,0)
		else:
			Screen.Colour = Colour(0,0.25,0) //.Green
		
		Screen.Expand = false
		
		// Add the + - * / buttons
		ops = ButtonBar(c)		// A button bar is a row that's made especially to hold buttons like a tool bar
		ops.AddItem("+", {Do("+")})
		ops.AddItem("-", {Do("-")})
		ops.AddItem("*", {Do("*")})
		ops.AddItem("/", {Do("/")})
		
		// Add a sub-column without a border to fit the rest of the buttons
		c2 = Column(c, false)
		c2.UniformSizing = true
		c2.Spacing = -4
		
		// Add the first row of buttons
		r = Row(c2, false)
		r.UniformSizing = true
		r.Spacing = 4
		Button(r, "7", {Do("7")})
		Button(r, "8", {Do("8")})
		Button(r, "9", {Do("9")})
		
		r = Row(c2, false)
		r.UniformSizing = true
		r.Spacing = 4
		Button(r, "4", {Do("4")})
		Button(r, "5", {Do("5")})
		Button(r, "6", {Do("6")})
		
		r = Row(c2, false)
		r.UniformSizing = true
		r.Spacing = 4
		Button(r, "1", {Do("1")})
		Button(r, "2", {Do("2")})
		Button(r, "3", {Do("3")})
		
		// The final row of buttons, including a clear button, and equals
		r = Row(c2, false)
		r.UniformSizing = true
		r.Spacing = 4
		Button(r, ".", {Do(".")})
		Button(r, "0", {Do("0")})
		Button(r, "=", {Do("=")})
		
		// Add a menu item to open a new calculator
		Menu.AddItem("Clear", {Do("CLR")})
		Menu.AddItem("Pi", {Screen.Text = System.Math.PI.ToString()})
		Menu.AddItem("New window", {CalcWindow()})
		
		// Resize the window to something appropriate
		Size = Size(300,400)
	
	// Draws the window
	/*override def Draw (b as Brush):
		b.LineWidth = 2
		b.Draw(Rect(Size))
		
		b.Colour = Colour.Black.Alpha(0.75)
		b.FillPreserve()
		
		b.Colour = Colour.White
		b.Stroke()
		
		DrawContents(b)*/
	
	// Called when a key is pressed
	override def KeyDown (c as char, k as Key):
		Do(c.ToString())
	
	// Called by buttons or when a key is pressed, to act upon it
	def Do (a as string):
		if a == "+":
			try:
				LastNumber = double.Parse(Screen.Text)
				Operation = "+"
				Screen.Text=""
			except e:
				pass
				
		elif a == "-":
			try:
				LastNumber = double.Parse(Screen.Text)
				Operation = "-"
				Screen.Text=""
			except e:
				pass
				
		elif a == "*":
			try:
				LastNumber = double.Parse(Screen.Text)
				Operation = "*"
				Screen.Text=""
			except e:
				pass
				
		elif a == "/":
			try:
				LastNumber = double.Parse(Screen.Text)
				Operation = "/"
				Screen.Text=""
			except e:
				pass
				
		elif a == "=":
			try:
				num = double.Parse(Screen.Text)
				result = num
				
				if Operation == "+":
					result = num + LastNumber
				elif Operation == "-":
					result = LastNumber - num
				elif Operation == "*":
					result = LastNumber * num
				elif Operation == "/":
					result = LastNumber / num
				
				Operation = ""
				Screen.Text = result.ToString()
			except e:
				pass
		
		elif a == "CLR":
			Screen.Text = "0"
			Operation = ""
			LastNumber = 0
		else:
			Screen.Text += a

// This is what is executed when the tool is opened.
class CalcTool (UI.Tool, IOpen):
	def Open ():
		CalcWindow()
